package org.millioncall.yueyoga.admin.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.millioncall.yueyoga.admin.model.Member;
import org.millioncall.yueyoga.admin.model.Status;
import org.millioncall.yueyoga.admin.model.Task;
import org.millioncall.yueyoga.admin.service.CheduleDetailsService;
import org.millioncall.yueyoga.admin.service.CheduleService;
import org.millioncall.yueyoga.admin.service.MemberService;
import org.millioncall.yueyoga.admin.service.ProductService;
import org.millioncall.yueyoga.admin.service.StatusService;
import org.millioncall.yueyoga.admin.service.SystemService;
import org.millioncall.yueyoga.admin.service.TaskService;
import org.millioncall.yueyoga.admin.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class ScheduleJobSet {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(ScheduleJobSet.class);

	private MemberService memberService;
	private TaskService taskService;
	private StatusService statusService;
	private UserService userService;
	private CheduleDetailsService cheduleDetailsService;
	private SystemService systemService;
	private CheduleService cheduleService;
	private ProductService productService;

	public MemberService getMemberService() {
		return memberService;
	}

	@Autowired
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	@Autowired
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public StatusService getStatusService() {
		return statusService;
	}

	@Autowired
	public void setStatusService(StatusService statusService) {
		this.statusService = statusService;
	}

	public UserService getUserService() {
		return userService;
	}

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public CheduleDetailsService getCheduleDetailsService() {
		return cheduleDetailsService;
	}

	@Autowired
	public void setCheduleDetailsService(
			CheduleDetailsService cheduleDetailsService) {
		this.cheduleDetailsService = cheduleDetailsService;
	}

	public SystemService getSystemService() {
		return systemService;
	}

	@Autowired
	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	public CheduleService getCheduleService() {
		return cheduleService;
	}

	@Autowired
	public void setCheduleService(CheduleService cheduleService) {
		this.cheduleService = cheduleService;
	}

	public ProductService getProductService() {
		return productService;
	}

	@Autowired
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public void doBiz() {
		List<Member> list = this.memberService.findExpireBirthdayMember();

		// 到期提醒
		Status status = this.statusService.findById(71);

		Task task = null;
		for (Member mem : list) {
			task = new Task();
			Date date = new Date();
			task.setCreatetime(date);
			task.setExpirydate(mem.getBirthday());
			task.setMember(mem);
			task.setMemberId(mem.getMemberid());
			task.setName("生日提醒！");
			task.setRemarks(mem.getName() + "30天内过生日！");
			task.setState(1);
			task.setChildStatus(status);
			task.setSubStatusId(status.getId());
			task.setType(1);
			task.setOrganizationId(mem.getOrganizationId());

			this.taskService.save(task);
		}
	}

	/**
	 * 自动开启预定
	 * 
	 * @throws ParseException
	 */
	public void doBizAutoReserve() throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar can = Calendar.getInstance();
		Date today = can.getTime();
		String strToday = sdf.format(today);

		can.add(Calendar.DAY_OF_MONTH, 1);
		Date tomorrow = can.getTime();
		String strTomorrow = sdf.format(tomorrow);

		// 开启今天预订
		Set<String> todayDateClassIds = cheduleService.findAllDateClassIds(sdf
				.parse(strToday));
		Set<String> todayWeekClassIds = cheduleService.findAllWeekClassIds(sdf
				.parse(strToday));
		for (String classId : todayDateClassIds) {
			cheduleService.reservateSeat(classId, null, strToday);
		}
		for (String classId : todayWeekClassIds) {
			cheduleService.reservateSeat(null, classId, strToday);
		}

		// 开启明天预订
		Set<String> tomorrowDateClassIds = cheduleService
				.findAllDateClassIds(sdf.parse(strTomorrow));
		Set<String> tomorrowWeekClassIds = cheduleService
				.findAllWeekClassIds(sdf.parse(strTomorrow));
		for (String classId : tomorrowDateClassIds) {
			cheduleService.reservateSeat(classId, null, strTomorrow);
		}
		for (String classId : tomorrowWeekClassIds) {
			cheduleService.reservateSeat(null, classId, strTomorrow);
		}
	}

	/** 判断课程是否已结束 */
	public void doBizScheduleOver() {
		Map<String, Object> map2 = new HashMap<String, Object>();
		Date today = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			map2.put("nowDate", sdf.parse(sdf.format(today)));
		} catch (ParseException e) {
			LOGGER.error("解析异常", e);
		}

		sdf = new SimpleDateFormat("HH:mm");
		try {
			map2.put("nowTime", sdf.parse(sdf.format(today)));
		} catch (ParseException e) {
			LOGGER.error("解析异常", e);
		}

		this.cheduleDetailsService.updateScheduleOver(map2);
	}

	/**
	 * 每周一将会员产品的本周使用次数清0
	 */
	public void autoClearWeeklyUseTimes() {
		this.productService.clearWeeklyUseTimes();
	}
}
